/***************************************************************************
 创建者: 华磊
 开始时间: 2019.4.24
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:暂时不支持多条同步带同时跟踪
 *
 *                                                                         *
 ***************************************************************************/

#ifndef BELTTRACKMANAGER_H
#define BELTTRACKMANAGER_H
//#include <QTcpSocket>
#include <QObject>
#include "globalDefine.h"
#include <QVector>
#include <QMap>
#include <QQueue>
#include <QTimer>
#include "MacroDefine/GeneralDefine.h"

//#define D_MAX_BELT_SIZE 20

class CoordinateManager;
class RegData;
class MotionMessage;
//class QTcpSocket;

class VisionTrack;
class BeltTrack;
class RobotIo;

#include "frames.hpp"
using namespace KDL;

class BeltTrackManager : public QObject
{
    Q_OBJECT
public:
    explicit BeltTrackManager(QString configFileIn, CoordinateManager *coordinateManagerIn, RegData *regDataIn,
                              MotionMessage *motionMessageIn, int robotIdIn, double deltaTimeIn,
                              RobotIo *ioControllerIn, QObject *parent = nullptr);

public:
    int setCurrentFrameCommand(int beltIndexIn,Frame frameIn);
    int addCeaseMotion(int beltIndexIn,Frame startFrameIn, Frame endFrameIn);
    int moveOneStep_FrameCommand(int beltIndexIn, Frame &frameOut, double &durationLeftOut);
    int setWork(bool isOnIn);
    int getCurrentTrackIndex();
    int getTrackTransionDistance_start(int beltIndexIn,double &deltaX,double &deltaY);
    int getTrackTransionDistance_end(int beltIndexIn,double &deltaX,double &deltaY);
    int setDebugFlag(int flagIn);
    int getEncoderValue_belt(int beltIndexIn,int &encoderValueOut);
    int getEncoderValue_vision(int visionIndexIn,int &encoderValueOut);
    int forceConnectCamera(int visionIndex);
    int forceTrigCamera(int visionIndexIn,bool isHighIn);
    int cease();
    int reset();
    bool isInitialOk();
    bool isUseThisModule();
    int setVisionTrigObjectLength(int visionIndex,double lengthIn);
    int setTrackOffset(int beltIndex, double offset_x, double offset_y);
    int getVisionTrackConfigList(std::vector<struct VisionTrackConfig> &configListOut);
    int getBeltTrackConfigList(std::vector<struct BeltTrackConfig> &configListOut);
    int modifyBeltTrackConfig(int beltIndex,BeltTrackConfig configIn);
    int modifyVisionTrackConfig(int visionIndex,VisionTrackConfig configIn);

    int getVisionTrackDebugInfo(int visionIndex,VisionTrackDebugInfo &visionInfoOut);
    int getBeltTrackDebugInfo(int beltIndex,BeltTrackDebugInfo &beltTrackInfoOut);

    int runVision(int visionIndex);//初始化视觉
    int trigVisionOnce(int visionIndex);//触发视觉一次
    int getVisionData(int visionIndex);//获取视觉点
    int clearVisionData(int visionIndex);//清除视觉缓冲区

    int getTrackData(int beltIndex);//根据筛选调节，设置当前跟踪点，获取ｐr点信息，同时一直根据该跟踪点的锁存编码器值更新用户坐标系的值。
    int trackStart(int beltIndex);//设定当前用户坐标系号为D_TRACK_USER_COORDINATE_INDEX
    int trackEnd(int beltIndex);

    int loopCommontimeOnce();//非实时
    int loopRealtimeOnce(QVector<PointPro> &deltaPoseOut, QVector<E_MOVE_STAGE> &isInHoldingStatusOut);//（跟踪点的锁存编码器值和当前编码器值更新用户坐标系的值），实时更新用户坐标系


private:

    int getEncoderValue_belt(BeltTrack* tmpBeltTrackIn, int &encoderDataOut);
    int initialVision(int visionIndex);
    int initialConfigFile(QString configFileName);
    int writeConfigFile(QString configFileName);
    void addMsg(int messageLevel, std::string componentName, std::string messageType, int messageCode
                , int robotIdIn, int parameter1=0, int parameter2=0);

    private slots:
    void timeOut_slot();
private:
    QString configFile;
    QVector<BeltTrackConfig> beltConfigList;
    QVector<VisionTrackConfig> visionConfigList;
    QMap<int ,VisionTrack*> visionTrackMap;
    QMap<int ,BeltTrack*> beltTrackMap;
    bool isInitialOkStatus;
    bool isUseTrack;
    CoordinateManager *coordinateManager;
    RegData *regData;
    MotionMessage *motionMessage;
    int robotId;
    double deltaTime;
    QTimer *timer;
    RobotIo *ioController;
    int currentTrackIndex;



};

#endif // BELTTRACKMANAGER_H
